package com.ypz.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ypz.reggie.common.CustomException;
import com.ypz.reggie.dto.SetmealDto;
import com.ypz.reggie.entity.Setmeal;
import com.ypz.reggie.entity.SetmealDish;
import com.ypz.reggie.mapper.SetmealMapper;
import com.ypz.reggie.service.SetmealDishService;
import com.ypz.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SermealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 保存套餐和套餐与菜品的关系
     * @param setmealDto
     */
    @Override
    @Transactional//操作多张表需要的
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐信息
        this.save(setmealDto);

        //保存套餐与菜品的关系

        //取出套餐与菜品的关系
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        //设置套餐id,setmealDto中原本没有id，保存套餐信息 到数据库后，会返回数据库为它生成的id
        for (SetmealDish setmealDish : setmealDishList) {

            setmealDish.setSetmealId(setmealDto.getId());

        }
        setmealDishService.saveBatch(setmealDishList);
    }

    /**
     * 删除套餐信息和与套餐关联的菜品信息
     * @param ids
     */
    @Override
    @Transactional//操作多张表需要的
    public void removeWithDish(List<Long> ids) {
        //查看套餐状态是否为在售卖中，是则不能删除
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        lqw.eq(Setmeal::getStatus,1);

        //执行查询
        int count = this.count(lqw);

        if (count > 0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //删除套餐，removeByIds这个方法是针对主键值的
        this.removeByIds(ids);

        //删除与关联菜品的信息，removeByIds这个方法是针对主键值的
        LambdaQueryWrapper<SetmealDish> setmealDishLqw = new LambdaQueryWrapper<>();
        setmealDishLqw.in(SetmealDish::getSetmealId, ids);

        setmealDishService.remove(setmealDishLqw);
    }

    /**
     * 更新套餐和套餐与菜品关系的信息
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional//操作多张表需要的
    public Boolean updateWithDish(SetmealDto setmealDto) {
        //更新套餐
        boolean update = this.updateById(setmealDto);

        //更新套餐与菜品的关系,分化为删除和保存
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        Long setmealId = setmealDto.getId();

        //setmealDishes里的元素没有套餐id，所以要设置套餐id
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmealId != null, SetmealDish::getSetmealId, setmealId);

        boolean remove = setmealDishService.remove(lqw);

        boolean saveBatch = setmealDishService.saveBatch(setmealDishes);

        if (update && remove && saveBatch){
            return true;
        }

        return false;
    }
}
